package com.ruoyi.crm.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.crm.domain.FollowRecord;
import com.ruoyi.crm.service.IFollowRecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 跟进记录Controller
 *
 * @author ruoyi
 * @date 2021-08-25
 */
@RestController
@RequestMapping("/record")
public class FollowRecordController extends BaseController
{
    @Autowired
    private IFollowRecordService followRecordService;

    /**
     * 查询跟进记录列表
     */
    @PreAuthorize(hasPermi = "crm:record:list")
    @GetMapping("/list")
    public TableDataInfo list(FollowRecord followRecord)
    {
        startPage();
        List<FollowRecord> list = followRecordService.selectFollowRecordList(followRecord);
        return getDataTable(list);
    }

    /**
     * 导出跟进记录列表
     */
    @PreAuthorize(hasPermi = "crm:record:export")
    @Log(title = "跟进记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FollowRecord followRecord) throws IOException
    {
        List<FollowRecord> list = followRecordService.selectFollowRecordList(followRecord);
        ExcelUtil<FollowRecord> util = new ExcelUtil<FollowRecord>(FollowRecord.class);
        util.exportExcel(response, list, "跟进记录数据");
    }

    /**
     * 获取跟进记录详细信息
     */
    @PreAuthorize(hasPermi = "crm:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(followRecordService.selectFollowRecordById(id));
    }

    /**
     * 新增跟进记录
     */
    @PreAuthorize(hasPermi = "crm:record:add")
    @Log(title = "跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FollowRecord followRecord)
    {
        return toAjax(followRecordService.insertFollowRecord(followRecord));
    }

    /**
     * 修改跟进记录
     */
    @PreAuthorize(hasPermi = "crm:record:edit")
    @Log(title = "跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FollowRecord followRecord)
    {
        return toAjax(followRecordService.updateFollowRecord(followRecord));
    }

    /**
     * 删除跟进记录
     */
    @PreAuthorize(hasPermi = "crm:record:remove")
    @Log(title = "跟进记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(followRecordService.deleteFollowRecordByIds(ids));
    }

    /**
     * 根据客户名称查询跟进记录
     */
    @PreAuthorize(hasPermi = "crm:record:queryByName")
    @GetMapping("/name/{name}")
    public AjaxResult selectByName(@PathVariable String name)
    {
        AjaxResult success = AjaxResult.success(followRecordService.selectByName(name));
        return success;
    }
}